// 将mutations（修改器）拆分出来，最后在抛出去
export default {
    ONCHANGEPLUS(state,item){//点击加号按钮
        state.cartList.forEach(el=>{
            if(el.item.id==item.id){
                el.num++
            }
        })
        localStorage.setItem("cartList",JSON.stringify(state.cartList));
    },
    ONCHANGEMINES(state,item){//点击减号按钮
        state.cartList.forEach(el=>{
            if(el.item.id==item.id){
                if(el.num<=0){
                    el.num=1
                }else{
                    el.num--;
                }
            }
        })
        localStorage.setItem("cartList",JSON.stringify(state.cartList));
    },
    ONADDGOODS(state,item){//点击添加购物车
        var rel = false;
        state.cartList.forEach(el=>{
            // console.log(el.item.id);
            // console.log(item.item.id);
            if(el.item.id==item.item.id){
                el.num++;
                rel = true;
            }
        })
        if(!rel){
            item.checked=false;//复选框默认状态
            state.cartList.push(item);
        }
        localStorage.setItem("cartList",JSON.stringify(state.cartList));
    },
    ONDELETE(state,item){//点击删除购物车数据
        state.cartList.forEach((el,index)=>{
            if(el.item.id==item.id){
                state.cartList.splice(index,1);
            }
        })
        localStorage.setItem("cartList",JSON.stringify(state.cartList));
    },
    ONCHANGECHECKBOX(state,item){//点击改变复选框的状态
        // console.log(item);
        // console.log(item.item.id);
        state.cartList.forEach(ele=>{
            // console.log(ele.item.id);
            if(ele.item.id===item.item.id){
                ele.checked = item.checked;
                // console.log(ele.checked);
            }
        })
        localStorage.setItem("cartList",JSON.stringify(state.cartList));
    },
    ONALLCHECKED(state,temp){//点击改变全选状态
        console.log(temp);
        state.cartList.forEach(ele=>{
            ele.checked = temp;
        })
        localStorage.setItem("cartList",JSON.stringify(state.cartList));
    }
}